<template>
  <base-breadcrumb :title="titleName">
    <searchData
      :search-data="searchArr"
      @search="getTableData"
    />
    <a-card :bordered="false">
      <list-table
        ref="table"
        row-key="id"
        :columns="columns"
        :request="loadData"
        class="lawyerLetter-table"
      >
        <template
          slot="prefix"
        >
          <a-button
            v-btnPermission="'lcm_001_add_02'"
            type="primary"
            @click="getAddLawyerLetter(false)"
          >
            录入函件
          </a-button>
          <excel
            ref="exportBtn"
            v-btnPermission="'lcm_001_exp_05'"
            file-name="西南公司函件台账"
            content-title="西南公司函件台账"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              type="primary"
              :loading="loadingState.isExport"
              @click="handleExport"
            >
              导出
            </a-button>
          </excel>
        </template>
        <template
          slot="letterStatus"
          slot-scope="text"
        >
          {{ resolveStatus(text) }}
        </template>
        <template
          slot="action"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-btnPermission="'lcm_001_loo_01'"
              @click="handleUpdate('look', record)"
            >查看</a>
            <a
              v-if="record.letterStatus === 'NO_OUTPUT' && record.creatorId === $userInfo().id"
              @click="getAddLawyerLetter(record.id)"
            >编辑</a>
            <a
              v-if="record.letterStatus === 'NO_OUTPUT'"
              v-btnPermission="'lcm_001_jz_03'"
              @click="handleUpdate('look', record, 'progress')"
            >进展情况</a>
            <a
              v-if="record.letterStatus === 'NO_OUTPUT'"
              v-btnPermission="'lcm_001_pz_04'"
              @click="handleUpdate('look', record, 'comment')"
            >批注</a>
            <a
              v-if="record.letterStatus === 'NO_OUTPUT' && record.creatorId === $userInfo().id"
              @click="getOutPut(record)"
            >销项</a>
          </MoreButton>
        </template>
        <div
          slot="expandedRowRender"
          slot-scope="record"
          style="margin: 0"
        >
          <p>
            主要内容：<span>{{ record.mainContent }}</span>
          </p>
        </div>
      </list-table>
    </a-card>
    <out-put
      :visible="visible"
      :loading="loading"
      title="销项"
      :form="chooseRow"
      @ok="getPost"
      @cancel="visible = false"
    />
  </base-breadcrumb>
</template>

<script>
import { getList, destroyLetter, exportData } from '@/api/legal/caseAndLetter/letterManage.js'
// import {
//     deleteByLawyerLetter,
//     // getTechnologyLinkage,
//     // getTechnologyProject,
//     postLawmanageOutPut

// } from '@/api/legal'
import outPut from '../outPut'
import STable from '@/components/Table'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import Selector from '@/components/Select/Selector'
import { CASE_STATUS_MAP } from '@/config/businessConfig.js'

export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    outPut
  },
  data() {
    return {
      loadingState: {
        isExport: false
      },
      titleName: undefined,
      rogerThatTime: undefined, //收到时间
      theTermTime: undefined, //期限
      queryParam: {},
      advanced: false,
      branchCompanyList: [], //所属分公司列表
      projectList: [], //项目列表
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters).then((res) => {
          return res
        })
      },
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   type: SelectSubCompany,
        //   valueKey: 'branchComId'
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId',
        //   keyValue: 'branchComId'
        // },
        {
          name: '状态',
          value: 'letterStatus',
          type: 'select',
          data: Object.keys(CASE_STATUS_MAP).map((key) => {
            return {
              label: CASE_STATUS_MAP[key],
              value: key
            }
          })
        },
        {
          name: '收到时间',
          value: 'getDate',
          type: 'dateRange',
          attr: {
            rangeFormat: ['getStartDate', 'getEndDate']
          }
        },
        {
          name: '涉及金额（万）',
          type: 'rangeInput',
          value: 'amount',
          attr: {
            rangeFormat: ['minAmount', 'maxAmount']
          }
        },
        {
          name: '函件编号',
          value: 'code',
          type: 'input'
        },
        {
          name: '函件类型',
          valueKey: 'legalTypeId',
          type: Selector,
          urlType: 'legal',
          customQuery: { needCount: false, query: { type: 2 } }
        }
      ],
      columns: [
        {
          title: '所属分公司',
          dataIndex: 'projectInfo.branchComName'
        },
        {
          title: '项目名称',
          dataIndex: 'projectInfo.projectName'
        },
        {
          title: '函件编号',
          dataIndex: 'code'
        },
        {
          title: '函件类型',
          dataIndex: 'legalType.name'
        },
        {
          title: '发函单位',
          dataIndex: 'seddingUnit'
        },
        {
          title: '涉及金额（万）',
          dataIndex: 'amountVO.amount'
        },
        {
          title: '收到时间',
          dataIndex: 'receivedDate'
        },
        {
          title: '状态',
          dataIndex: 'letterStatus',
          scopedSlots: { customRender: 'letterStatus' }
        },
        {
          title: '操作',
          width: '160px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      visible: false,
      loading: false,
      action: false, //操作按钮下拉展示
      form: ['', ''],
      chooseRow: null
    }
  },
  methods: {
    handleUpdate(pageType, data = {}, updateType = '') {
      const commonHandler = () => {
        this.$router.push({
          path: '/legal/caselawyerLetter/lawyerLetter/update',
          query: {
            id: data.id || '',
            pageType,
            updateType: updateType
          }
        })
      }
      const map = {
        look: commonHandler
      }
      map[pageType] && map[pageType]()
    },
    /**
     * 解析状态值
     */
    resolveStatus(status) {
      return CASE_STATUS_MAP[status] || ''
    },
    /**
     * 导出搜索条件下所有数据
     */
    async handleExport() {
      try {
        if (this.loadingState.isExport) return
        this.loadingState.isExport = true
        const res = await exportData({ needCount: false, ...this.queryParam })
        const list = (res.data || []).map((item, index) => {
          item.index = index + 1
          item.letterStatus = this.resolveStatus(item.letterStatus)
          item.amount = item?.amountVO?.amount ? item.amountVO.amount : 0
          return item
        })
        const columns = [
          {
            title: '序号',
            dataIndex: 'index'
          },
          {
            title: '所属分公司',
            dataIndex: 'projectInfo.branchComName'
          },
          {
            title: '所属项目',
            dataIndex: 'projectInfo.projectName'
          },
          {
            title: '函件种类',
            dataIndex: 'legalType.name'
          },
          {
            title: '函件编号',
            dataIndex: 'code'
          },

          {
            title: '发函单位',
            dataIndex: 'seddingUnit'
          },
          {
            title: '涉及金额（万）',
            dataIndex: 'amount'
          },
          {
            title: '收到时间',
            dataIndex: 'receivedDate'
          },
          {
            title: '处理状态',
            dataIndex: 'letterStatus'
          },
          {
            title: '创建人',
            dataIndex: 'creatorName'
          },
          {
            title: '创建时间',
            dataIndex: 'createTime'
          },
          {
            title: '销项时间',
            dataIndex: 'destroyed.createTime'
          },
          {
            title: '销项说明',
            dataIndex: 'destroyed.comment'
          }
        ]
        this.$refs.exportBtn.generateExcel(list, { columns })
      } catch (e) {
        console.error(e)
      }
      this.loadingState.isExport = false
    },
    getTableData(val) {
      // return this.$console(val)
      val && (this.queryParam = val)
      this.$refs.table.refresh()
    },
    getAddLawyerLetter(id) {
      if (id) {
        this.$router.push({
          path: '/legal/caselawyerLetter/lawyerLetter/edit',
          query: {
            id: id
          }
        })
      } else {
        this.$router.push({
          path: '/legal/caselawyerLetter/lawyerLetter/edit'
        })
      }
    },
    getDetails(id) {
      this.$router.push({
        path: '/legal/caselawyerLetter/lawyerLetter/details',
        query: {
          id: id
        }
      })
    },
    getOutPut(record) {
      this.visible = true
      this.outPutId = record.id
      this.chooseRow = record

      this.loading = false
    },
    getPost(subQueryParam) {
      subQueryParam.lawyerId = this.outPutId
      subQueryParam.fileInfoList = (subQueryParam.fileInfoList && subQueryParam.fileInfoList.map((item) => item.commonId)) || []
      this.loading = true
      destroyLetter(subQueryParam)
        .then((res) => {
          this.loading = false
          if (res.data.code === 200) {
            this.visible = false
            this.$message.success('提交成功')
            this.$refs.table.refresh()
          }
        })
        .catch((err) => {
          this.loading = false
        })
    },
    getReskManagement(id) {
      this.$router.push({
        path: '/legal/caselawyerLetter/lawyerLetter/riskManagement',
        query: {
          id: id
        }
      })
    },
    getUpdate(id) {
      this.$router.push({
        path: '/legal/caselawyerLetter/lawyerLetter/update',
        query: {
          id: id
        }
      })
    }
  }
}
</script>


<style lang="less" scoped>
/deep/ .table-page-search-wrapper .ant-form-inline .ant-form-item > .ant-form-item-label {
  min-width: 90px;
}

/deep/ .ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  padding: 10px;
}
</style>
